package com.ftg.learn.bus.basicinfo_record.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.basicinfo_record.entity.BasicinfoRecord;
import com.ftg.learn.bus.basicinfo_record.mapper.BasicInfoRecordMapper;
import com.ftg.learn.bus.util.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BasicInfoRecordService {

    @Autowired
    BasicInfoRecordMapper birm;

    /**
     * 查询
     * @return
     */
    public List<BasicinfoRecord> showAll() {
        return birm.showAll();
    }

    /**
     * 删除单条
     * @param id
     * @return
     */
    public int delete(String id) {
        if(id == null || id.equals("")){
            return -1;
        }else{
            return birm.deleteById(id);
        }
    }

    /**
     * 查一条记录
     * @param id
     * @return
     */
    public BasicinfoRecord findOne(String id) {
        return birm.selectById(id);
    }

    /**
     * 基础操作添加
     * @return
     */
    public int BasicInsertRecord(String id,String name){
        String uid = "在用户表中添加一条数据,影响数据用户名:"+id;
        return birm.autoInsert(uid,name);
    }

    public int BasicInsertRoleRecord(String id,String name){
        String uid = "在角色表中添加一条数据,影响角色名:"+id;
        return birm.autoInsert(uid,name);
    }

    /**
     * 基础操作修改
     * @return
     */
    public int BasicUpdateRecord(String id,String name){
        String uid = "在用户表中修改一条数据,影响数据用户名:"+id;
        return birm.autoInsert(uid,name);
    }

    public int BasicUpdateRoleRecord(String id,String name){
        String uid = "在角色表中修改一条数据,影响角色名:"+id;
        return birm.autoInsert(uid,name);
    }

    /**
     * 基础操作删除(未启用)
     * @return
     */
    public int BasicdeleteRecord(String id,String name){
        String uid = "在用户表中删除一条数据,影响数据用户名:"+id;
        return birm.autoInsert(uid,name);
    }


    /**
     * 分页查询
     * @param page              总页数
     * @param limit             每页显示条数
     * @param basic_id          基础操作id
     * @param basic_operater    操作人
     * @return
     */
    public ResultType<BasicinfoRecord> query(int page, int limit, String basic_id, String basic_operater){

        ResultType<BasicinfoRecord> rt = new ResultType<>();
        rt.setCode(0);

        IPage<BasicinfoRecord> ip = new Page<>(page,limit);

        QueryWrapper<BasicinfoRecord> qw = new QueryWrapper<>();
        qw.and(wrapper -> wrapper.like("basic_id",basic_id).like("basic_operater",basic_operater));

        ip =  birm.selectPage(ip,qw);
        rt.setData(ip.getRecords());
        rt.setCount(ip.getTotal());
        return rt;
    }
}
